package com.hlmc.spider;

import com.hlmc.config.Constants;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;

import javax.annotation.Resource;
@Service("total_360spider")
public class DyDmTv_spider extends BaseProcessor {

    @Resource
    private DS_pipeline ds_pipeline;

    private Site site = Site.me().setRetryTimes(3).setSleepTime(100).setCharset("utf-8" + "").setTimeOut(8000);

    @Override
    public void process(Page page) {
        page.addTargetRequests(page.getHtml().xpath("//a[@class='js-tongjip']/@href").all());
        String str_tv="https://www.360kan.com/dianshi/list.php?cat=";
        String str_dy="https://www.360kan.com/dianying/list.php?cat=";
        String str_dm="https://www.360kan.com/dongman/list.php?cat=";
        if(page.getUrl().toString().contains(str_tv)) {
            page.addTargetRequests(page.getHtml().css("#js-ew-page a:last-child", "href").all());

            page.addTargetRequests(page.getHtml().xpath("//div[@class='s-tab-main']/ul/li/a/@href").all());
        }
            String model_ = "//div[@class='s-top g-clear s-top-dianshi']";
            String model_r = "//div[@class='s-top g-clear s-top-dianshi']/div[@class='s-top-right']/div[@class='s-top-info']";
            if (page.getUrl().regex("https://www.360kan.com/tv/\\w{14}\\.html").match()) {
                String ds_title = page.getHtml().xpath(model_r + "/div[1]/div[@class='title-left g-clear']/h1/text()").toString().replace(" ", "");
                String image = page.getHtml().xpath(model_ + "/div[@class='s-top-left']/div/a/img/@src").toString();
                String score = page.getHtml().xpath(model_r + "/div[1]/div[@class='s-title-right']/span/text()").toString();
                String toup = page.getHtml().xpath(model_r + "/div[1]/div/p/html()").toString().replace("<span>", "").replace("</span>", "");
                ;
                String type = page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[1]/a/text()").all().toString().replace("[", "").replace("]", "");
                String year = page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[2]/text()").toString().replace("[", "").replace("]", "");
                String area = page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[3]/text()").toString().replace("[", "").replace("]", "");
                String director = page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[5]/a/text()").toString().replace("[", "").replace("]", "");
                String actor = page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[6]/a/text()").all().toString().replace("[", "").replace("]", "");
                String detail = page.getHtml().xpath("//div[@id='js-desc-switch']/div[3]/p/text()").toString().replace("[", "").replace("]", "");
                String player_url_1 = page.getHtml().xpath("//div[@class='num-tab js-tabs']/div[2]/a/@href").all().toString().replace("[", "").replace("]", "").replace(" ", "");
                String player_url_2 = page.getHtml().xpath("//div[@class='num-tab js-tabs']/div/a/@href").all().toString().replace("[", "").replace("]", "").replace(" ", "");
                //System.out.println(ds_title);
                String player_url = player_url_1;
                if (player_url_1 == null || player_url_1.equals("")) {
                    player_url = player_url_2;
                }
                if (image != null && ds_title != null && !ds_title.equals("") && !image.equals("")) {
                    page.putField("title", ds_title);
                    page.putField("toup", toup);
                    page.putField("image", image);
                    page.putField("score", score);
                    page.putField("type", type);
                    page.putField("year", year);
                    page.putField("area", area);
                    page.putField("director", director);
                    page.putField("actor", actor);
                    page.putField("detail", detail);
                    page.putField("purl", player_url);
                    page.putField("category", Constants.CATEGORY_1);
                }
                if ((page.getResultItems().get("title") == null) || (page.getResultItems().get("title").toString()).replace(" ", "").equals("")) {

                    page.setSkip(true);
                }
            }
        if(page.getUrl().toString().contains(str_dy)) {
            page.addTargetRequests(page.getHtml().css("#js-ew-page a:last-child", "href").all());

            page.addTargetRequests(page.getHtml().xpath("//div[@class='s-tab-main']/ul/li/a/@href").all());
        }
            String model1_="//div[@class='c-top-main']/div";
            String model1_r=model_+"/div[2]/div";
            if(page.getUrl().regex("https://www.360kan.com/m/\\w{14}\\.html").match()) {

                //String ds_title=page.getHtml().xpath(model_r+"/div[1]/div/h1/text()").toString().replace(" ","");
                String ds_title = page.getHtml().xpath("//div[@class='title-left g-clear']/h1/text()").toString().replace(" ", "");
                String image = page.getHtml().xpath(model1_ + "/div[1]/div/a/img/@src").toString();
                String score = page.getHtml().xpath(model1_r + "/div[1]/div/span[1]/text()").toString();

                String type = page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[1]/a/text()").all().toString().replace("[", "").replace("]", "");
                String year = page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[2]/text()").toString().replace("[", "").replace("]", "");
                String area = page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[3]/text()").toString().replace("[", "").replace("]", "");
                String director = page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[5]/a/text()").toString().replace("[", "").replace("]", "");
                String actor = page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[6]/a/text()").all().toString().replace("[", "").replace("]", "");
                String detail = page.getHtml().xpath("//div[@id='js-desc-switch']/div[3]/p/text()").toString().replace("[", "").replace("]", "");
                String player_url = page.getHtml().xpath("//div[@id='js-site-wrap']/a[1]/@href").all().toString().replace("[", "").replace("]", "");

                System.out.println(ds_title);
                if (image != null && ds_title != null && !ds_title.equals("") && !image.equals("")) {

                    page.putField("title", ds_title);
                    page.putField("image", image);
                    page.putField("score", score);
                    page.putField("type", type);
                    page.putField("year", year);
                    page.putField("area", area);
                    page.putField("director", director);
                    page.putField("actor", actor);
                    page.putField("detail", detail);
                    page.putField("purl", player_url);
                    page.putField("category", Constants.CATEGORY_0);

                }

                if ((page.getResultItems().get("title") == null) || (page.getResultItems().get("title").toString()).replace(" ", "").equals("")) {

                    page.setSkip(true);
                }

        }
        if(page.getUrl().toString().contains(str_dm))
        {
            page.addTargetRequests( page.getHtml().css("#js-ew-page a:last-child","href").all());

            page.addTargetRequests(page.getHtml().xpath("//div[@class='s-tab-main']/ul/li/a/@href").all());
        }

        String model2_="//div[@class='c-top-main']/div";
        String model2_r=model_+"/div[2]/div";

        if(page.getUrl().regex("https://www.360kan.com/ct/\\w{14}\\.html").match()){
            String detail_1="";
            String detail_2="";
            detail_1=page.getHtml().xpath("//div[@class='item-desc-wrap g-clear js-open-wrap']/p/text()").toString().replace(" ","");
            detail_2=page.getHtml().xpath("//div[@class='item-desc-wrap g-clear js-close-wrap']/p/text()").toString().replace(" ","");
            String player_url1=page.getHtml().xpath("//div[@class='js-series-all m-series-number-container']/div/[@href!='###']/@href").all().toString().replace("[","").replace("]","").replace(" ", "");
            String player_url2=page.getHtml().xpath("//div[@class='m-series-content js-tab-content']/div/a/@href").all().toString().replace("[","").replace("]","").replace(" ", "");;
            String toup=page.getHtml().xpath(model2_r+"/div[1]/div/p/html()").toString().replace("<span>","").replace("</span>","");;
            String ds_title=page.getHtml().xpath(model2_r+"/div[1]/div/h1/text()").toString().replace(" ","");
            String image= page.getHtml().xpath(model2_+"/div[1]/div/a/img/@src").toString();
            String score=page.getHtml().xpath(model2_r+"/div[1]/div/span[1]/text()").toString();
            String type=page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[1]/a/text()").all().toString().replace("[","").replace("]","");
            String year=page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[2]/text()").toString().replace("[","").replace("]","");
            String area=page.getHtml().css(".area","text").toString().replace("[","").replace("]","");
            Object director1=page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[5]/a/text()");
            String director="";
            if(director1 !=null){
                director=director1.toString();
            }
            String actor=page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[6]/a/text()").all().toString().replace("[","").replace("]","");
            String detail="";
            if(detail_1.length()>=detail_2.length()){
                detail=detail_1;
            }else {
                detail=detail_2;
            }
            String player_url=player_url1;
            if(player_url==null||player_url.equals(""))
            {
                player_url=player_url2;
            }
            if(player_url.length()<player_url2.length()){
                player_url=player_url2;
            }
            if(image!=null&&ds_title!=null&&!ds_title.equals("")&&!image.equals("")) {

                page.putField("toup", toup);
                page.putField("title", ds_title);
                page.putField("image", image);
                page.putField("score", score);
                page.putField("type",type);
                page.putField("year",year);
                page.putField("area",area);
                page.putField("director",director);
                page.putField("actor",actor);
                page.putField("detail",detail);
                page.putField("purl",player_url);
                page.putField("category", Constants.CATEGORY_3);

            }

            if( (page.getResultItems().get("title") == null)||(page.getResultItems().get("title").toString()).replace(" ", "").equals("")) {

                page.setSkip(true);
            }

        }


    }


    @Override
    public Site getSite() {
        return site;
    }


}
